import {
  setCustomReportApi,
  getDsListApi,
  getHaveDsApi,
  setReportDsApi,
  getUpdateReportDsApi,
  deleteReportDsApi,
  getReportIdApi,
  updateCustomReportApi,
  get_datasourceListApi,
  getBeUsingReoport,
  getAllDatasetApi,
  deleteDataSetApi
} from '@/api/customReport'

import { addDSListApi, updateDSListApi } from '@/api/DSManage'

const custom = {
  namespaced: true,
  state: {
    // config信息
    reportForm: {},
    // 已有数据集 渲染穿梭框
    haveDs: [],
    // 数据集集合
    DsList: [],
    // 报表信息
    report: {},
    rulesBoolean: false,
    datasource_list: []
  },
  mutations: {
    // 设置新加数据集信息
    SET_REPORTFORM: (state, form) => {
      state.reportForm = {...state.reportForm, ...form}
    },
    UPDATE_REPORTFORM: (state, form) => {
      state.reportForm = form
    },
    // 设置报表信息
    SET_REPORT: (state, form) => {
      state.report = {...form}
    },
    // 设置已有数据集
    SET_HAVE_DS: (state, ds) => {
      state.haveDs = ds.data
    },
    // 设置数据集集合
    SET_DS_LIST: (state, list) => {
      let set = new Set(list);
      state.DsList = Array.from(set)
    },
    SET_RULESBOOLEAN: (state, bln) => {
      state.rulesBoolean = bln
    },
    SET_FILTER: (state, table) => {
      state.reportForm.filter = table
    },
    ADD_SET_LIST: (state, obj) => {
      state.DsList.push({ ...obj })
    },
    SET_DATASOURCE_LIST: (state, list) => {
      state.datasource_list = list
    }
  },
  actions: {
    // 获取正在使用该数据集的报表列表
    get_be_using_report ({}, id) {
      return new Promise((resolve, reject) => {
        getBeUsingReoport(id)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取用户可选项数据源
    get_userSelect_datasource ({ commit }, ) {
      return new Promise((resolve, reject) => {
        get_datasourceListApi()
          .then(res => {
            if (res.code == 0) {
              commit('SET_DATASOURCE_LIST', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 修改数据集
    updateData ({state}, prop) {
      let update_data = {},
          form = {...state.reportForm}
      if (form.type == 4) {
        if (form.config.time.duration == 'custom') {
          form.config.time.duration = form.config.time.customDuration;
          delete form.config.time.customDuration
        } else {
          delete form.config.time.customDuration
        }
        let isWastage = form.config.config.is_wastage == 1 ? true: false;
        form.config.config.is_wastage = isWastage;
      }
      if (form.type == 2) {
        if (form.config.config.dimension == undefined) {
          form.config.config.dimension = ''
        }
      }
      for(let key in form){
        if (typeof form[key] == 'string' || !!form[key] == false){
          update_data[key] = form[key]
        } else {
          update_data[key] = JSON.stringify(form[key])
        }
      }
      update_data.reportId = prop.reportId
      return new Promise((resolve, reject) => {
        updateDSListApi({...update_data})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 新建数据集
    saveData ( {state,commit},prop ) {
      const data = {reportId: prop.reportId},
            form = {...state.reportForm}
      if (form.type == 4) {
        if (form.config.time.duration == 'custom') {
          form.config.time.duration = form.config.time.customDuration;
          delete form.config.time.customDuration
        } else {
          delete form.config.time.customDuration
        }
        let isWastage = form.config.config.is_wastage == 1 ? true: false;
        form.config.config.is_wastage = isWastage;
      }
      if (form.type == 2) {
        if (form.config.config.dimension == undefined) {
          form.config.config.dimension = ''
        }
      }
      delete form.time
      for(let key in form){
        if (typeof form[key] == 'string' || !!form[key] == false){
          data[key] = form[key]
        } else {
          data[key] = JSON.stringify(form[key])
        }
      }
      data.reportId = prop.reportId
      return new Promise((resolve, reject) => {
        addDSListApi(data)
          .then(res => {
            if (res.code == 0) {
              commit('ADD_SET_LIST', {id: res.data.id})
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 修改报表
    updateReport ({ commit }, form) {
      return new Promise((resolve, reject) => {
        updateCustomReportApi({...form})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 查询指定报表
    getReportId ({ commit }, id) {
      return new Promise((resolve, reject) => {
        getReportIdApi({id})
          .then(res => {
            if (res.code == 0) {
              commit('SET_REPORT', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取修改报表数据集
    getUpdateReportDs ({commit}, id) {
      return new Promise((resolve, reject) => {
        getUpdateReportDsApi({...id})
          .then(res => {
            if (res.code == 0) {
              commit('UPDATE_REPORTFORM', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 删除报表数据集
    deleteReportDs ({}, id) {
      return new Promise((resolve, reject) => {
        deleteReportDsApi({...id})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 设置报表数据集
    setReportDs ({ state }, id) {
      const body = []
      state.DsList.forEach(ds=>{
        body.push(ds.id)
      })
      return new Promise((resolve, reject) => {
        setReportDsApi({body, id: id})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 创建报表
    setReport ({ commit }, form){
      return new Promise((resolve, reject) => {
        setCustomReportApi(form)
          .then(res => {
            if (res.code == 0) {
              commit('SET_REPORT', {...res.data})
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取报表数据集
    getDsList ({commit, state}, id) {
      return new Promise((resolve, reject) => {
        getDsListApi(id)
          .then(res => {
            if (res.code == 0) {
              commit('SET_DS_LIST',res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getAllDataset ({ commit }) {
      return new Promise((resolve, reject) => {
        getAllDatasetApi()
          .then(res => {
            if (res.code === 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取已有报表数据集
    getHaveDs ({ commit }) {
      return new Promise((resolve, reject) => {
        getHaveDsApi()
          .then(res => {
            if (res.code == 0) {
              commit('SET_HAVE_DS', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    deleteDataSet({commit, state},prop) {
      return new Promise((resolve, reject) => {
        deleteDataSetApi(prop.datasetId)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    }
  },
  getters: {

  },
}

export default custom
